package org.xobo.finance.helper.invoice.service.impl;

import java.math.BigDecimal;
import java.util.Collection;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.xobo.finance.helper.company.entity.Company;
import org.xobo.finance.helper.invoice.service.InvoiceCompanyCalcService;
import org.xobo.finance.helper.invoice.service.InvoiceCompanyService;

@Service
public class InvoiceCompanyServiceImpl implements InvoiceCompanyService {

  @Override
  public void calcCompanyInvoiceAmount(Collection<Company> companyList) {
    if (CollectionUtils.isEmpty(companyList)) {
      return;
    }

    Collection<Long> companyIdList =
        companyList.stream().map(Company::getId).collect(Collectors.toSet());

    Map<Long, BigDecimal> companyRecentSalesAmountMap =
        invoiceCompanyCalcService.calcCompanyRecentSalesAmount(companyIdList);

    BigDecimal totalAmount = new BigDecimal(5000000);

    companyList.forEach(company -> {
      BigDecimal amount = companyRecentSalesAmountMap.get(company.getId());
      if (amount == null) {
        amount = BigDecimal.ZERO;
      }
      company.setRecentSalesAmount(amount);
      company.setAvaliableAmount(totalAmount.subtract(amount));
    });
  }
  


  @Autowired
  private InvoiceCompanyCalcService invoiceCompanyCalcService;
}
